import {getUrlParam,filterData} from "@/static/js/filter.js";
export default {
	data () {
		return {
			params: {
				year: true,
				month: true,
				day: true,
				hour: true,
				minute: true,
				second: true
			},
			cxTimeparams:{
				hour: true,
				minute: true
			},
			id: '',
			form: {},
			motionArr:[
				{
					value: '1',
					label: '步行'
				},
				{
					value: '2',
					label: '慢速跑'
				},
				{
					value: '3',
					label: '中速跑'
				},
				{
					value: '4',
					label: '快速跑'
				},
				{
					value: '5',
					label: '游泳'
				},
				{
					value: '6',
					label: '瑜伽'
				},
				{
					value: '7',
					label: '舞蹈'
				},
				{
					value: '8',
					label: '跳绳'
				},
				{
					value: '9',
					label: '羽毛球'
				},
				{
					value: '10',
					label: '乒乓球'
				},
				{
					value: '11',
					label: '篮球'
				},
				{
					value: '12',
					label: '排球'
				}],
			bloodtimeArr:[
				{
					value: '1',
					label: '清晨空腹'
				},
				{
					value: '2',
					label: '早餐后'
				},
				{
					value: '3',
					label: '午餐前'
				},
				{
					value: '4',
					label: '午餐后'
				},
				{
					value: '5',
					label: '晚餐前'
				},
				{
					value: '6',
					label: '晚餐后'
				}],
			bloodmedicationArr:[
				{
					value: '1',
					label: '未用药'
				},
				{
					value: '2',
					label: '已用药'
				}],
			nsArr:[
				{
					value: '1',
					label: '未用药'
				},
				{
					value: '2',
					label: '已用药'
				}],
			stayTimeTime:'0:30',
			minDate: new Date(2020, 0, 1),
			maxDate: new Date(2020, 0, 1),
			currentDate: new Date(),
			minDate2: new Date(2020, 0, 1),
			maxDate2: new Date(2020, 0, 1),
			currentDate2: new Date(),
			start: '',
			end: '',
			stayTimeShow:false,
			dateShow:false,
			dateShow2:false,
			selectShow:false,
			columns:[],
			selectType:'',
			dateType:'',
			weight:'', //体重
			height:'', //身高
			systolicpressure:'', //收缩压
			diastolicpressure:'', //舒张压
			bloodglucose: '', //血糖值
			bloodtime: '' , //血糖记录时间
			bloodmedication:'', //血糖用药
			uric:'', //尿酸值
			uricmedication:'' , //尿酸用药
			heartrate:'' , //心率
			sleepStart:'' , //睡眠开始时间
			sleepEnd:'' , //睡眠结束时间
			motion:'', //运动类型
			motionTime:'', //运动时间
			defaultTime:''  ,//默认开始选中时间
			defaultArr: [1,2,3], //默认下拉框数组
			dropDownshow: false, //下拉框默认一开始隐藏
			confirmButton: '',
			cxTimeShow: false, //持续时间
			startShow: false,
			endShow: false,
		}
	},
	onready() {
		this.defaultTime = this.currentDate;
	},
	onLoad() {
		this.id = getUrlParam('id');
		this.form.singDate = filterData(this.currentDate.getTime());	
		if(this.id == 2) {		
			uni.setNavigationBarTitle({
				title: "记血压"
			});
		} else if(this.id == 3) {
			uni.setNavigationBarTitle({
				title: "记血糖"
			});
		} else if(this.id == 4) {
			uni.setNavigationBarTitle({
				title: "记尿酸"
			});
		}else if(this.id == 5) {
			uni.setNavigationBarTitle({
				title: "记心率"
			});
		}else if(this.id == 6) {
			uni.setNavigationBarTitle({
				title: "记睡眠"
			});
		}else if(this.id == 7) {
			uni.setNavigationBarTitle({
				title: "记运动"
			});
		}
	},
	methods: {
		// 选择弹出
		popUp(e) {
			this.dropDownshow = true;
			this.confirmButton = e;
			this.defaultArr = e == 'bloodtimeArr' ? this.bloodtimeArr : 
			this.defaultArr	= e == 'bloodmedicationArr' ? this.bloodmedicationArr : 
			this.defaultArr	= e == 'nsArr' ? this.nsArr : 
			this.defaultArr = e == 'motionArr' ? this.motionArr : []
		
		},
		// 下拉框确定
		dropDownconfirm(e) {
			console.log(e)
			this.confirmButton == 'bloodtimeArr' ? this.bloodtime = e[0].label : 
			this.confirmButton == 'bloodmedicationArr' ? this.bloodmedication = e[0].label : 
			this.confirmButton == 'nsArr' ? this.uricmedication = e[0].label : 
			this.confirmButton == 'motionArr' ? this.motion = e[0].label : ''
 			// this.bloodtime = e == 'bloodtimeArr' ? e[0].label : ''
			// console.log(this.confirmButton)
		},
		confirm(e) {
			const time = e.year+'-'+e.month+'-'+e.day+' '+e.hour+':'+e.minute+':'+e.second;
			if(new Date(time).getTime() > this.currentDate.getTime()) {
				uni.showToast({
					title:'记录时间选择不能大于当前系统时间',
					icon:'none'
				})
				return
			}
			this.form.singDate = e.year+'-'+e.month+'-'+e.day+' '+e.hour+':'+e.minute+':'+e.second;
		},
		startconfirm(e) {
			const timeTwo = e.year+'-'+e.month+'-'+e.day+' '+e.hour+':'+e.minute+':'+e.second;
			if(new Date(timeTwo).getTime() > this.currentDate.getTime()) {
				uni.showToast({
					title:'开始时间选择不能大于当前系统时间',
					icon:'none'
				})
				return
			}
			this.sleepStart = e.year+'-'+e.month+'-'+e.day+' '+e.hour+':'+e.minute+':'+e.second
		},
		endconfirm(e) {
			const sleepEnd = e.year+'-'+e.month+'-'+e.day+' '+e.hour+':'+e.minute+':'+e.second;	
			if(new Date(sleepEnd).getTime() > this.currentDate.getTime()) {
				uni.showToast({
					title:'结束时间选择不能大于当前系统时间',
					icon:'none'
				})
				return
			}else if(new Date(sleepEnd).getTime() - new Date(this.sleepStart).getTime() < 600000) {
				uni.showToast({
					title: '结束时间要比开始时间多十分钟',
					icon: 'none'
				})
				return				
			} else if(new Date(sleepEnd).getTime() - new Date(this.sleepStart).getTime() > 86400000) {
				uni.showToast({
					title: '睡眠时间最大选择范围在24小时以内哦',
					icon: 'none'
				})
				return
			}
			this.sleepEnd = sleepEnd;
		},
		cxTimeConfirm(e) {
			this.motionTime = e.hour+'小时'+e.minute+'分钟';
			console.log(this.form.motionTime)
		},
		endTime() {
			if(!this. sleepStart) {
				uni.showToast({title:'请选择开始时间',icon:'none'});
				return
			}
			this.endShow = true;
		},
		submit(){
			if(this.id == 1) {
				if(!this.weight) {
					uni.showToast({title:'请输入体重',icon:'none'});
					return
				}
				if(!this.height) {
					uni.showToast({title:'请输入身高',icon:'none'});
					return
				}
			}else if(this.id ==2) {
				if(!this.systolicpressure) {
					uni.showToast({title:'请输入收缩压',icon:'none'});
					return
				}
				if(!this.diastolicpressure) {
					uni.showToast({title:'请输入舒张压',icon:'none'});
					return
				}
			} else if(this.id == 3) {
				if(!this.bloodglucose) {
					uni.showToast({title:'请输入血糖值',icon:'none'});
					return
				}
				if(!this.bloodtime) {
					uni.showToast({title:'请选择测量时段',icon:'none'});
					return
				}
				if(!this.bloodmedication) {
					uni.showToast({title:'请选择用药情况',icon:'none'});
					return
				}
			} else if(this.id == 4) {
				if(!this. uric) {
					uni.showToast({title:'请输入尿酸值',icon:'none'});
					return
				}
				if(!this.uricmedication) {
					uni.showToast({title:'请选择用药情况',icon:'none'});
					return
				}
			} else if(this.id == 5) {
				if(!this. heartrate) {
					uni.showToast({title:'请输入心率值',icon:'none'});
					return
				}
			} else if(this.id == 6) {
				if(!this. sleepStart) {
					uni.showToast({title:'请选择开始时间',icon:'none'});
					return
				}
				if(!this.sleepEnd) {
					uni.showToast({title:'请选择结束时间',icon:'none'});
					return
				}
			} else if(this.id == 7) {
				if(!this. motion) {
					uni.showToast({title:'请选择运动类型',icon:'none'});
					return
				}
				if(!this.motionTime) {
					uni.showToast({title:'请选择运动时间',icon:'none'});
					return
				}
			}
			const smStart = new Date(this.sleepStart).getTime();
			const smEnd = new Date(this.sleepEnd).getTime();
			const parameterValues = 
				this.id == 1? 
					[{name:'身高',value:this.height},{name:'体重',value:this.weight}] :
				this.id == 2? 
					[{name:'收缩压',value:this.systolicpressure},{name:'舒张压',value:this.diastolicpressure}] : 
				this.id == 3?    
					[{name:'血糖值',value:this.bloodglucose},{name:'记录时间',value:this.bloodtime},{name:'用药情况',value:this.bloodmedication}] : 
				this.id == 4?    
					[{name:'尿酸值',value:this.uric},{name:'用药情况',value:this.uricmedication}] : 
				this.id == 5?    
					[{name:'心率',value:this.heartrate}] : 
				this.id == 6?    
					[{name:'开始时间',value:smStart},{name:'结束时间',value:smEnd}] : 
				this.id == 7?    
					[{name:'运动类型',value:this.motion},{name:'运动时间',value:this.motionTime}] : ''
				
			this.form.parameterValues = parameterValues
			this.form.parent = this.id;

			// return console.log(this.form)
			this.$api.AppMonitorSave(this.form).then((res) => {
				if(res.data.returnCode == '200') {
					uni.showToast({
						title: '记录成功',
						icon: 'none'
					})
					setTimeout(() => {
						uni.navigateBack({
							delta:1
						})
					},1000)
				} else {
					uni.showToast({
						title: '记录失败',
						icon: 'none'
					})
				}
				
			})
			// server.post('/AppMonitor/save?token='+localStorage.token,this.form).then(res=>{
			// 	if(res.data.StatusCode == 200){
			// 		uni.showToast({title:'记录成功'),icon:'none'}
			// 		window.history.go(-1)
			// 	}else{
			// 		vant.Toast('记录失败')
			// 	}
			// })
		}
	}
}